package com.it.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.commonutils.JwtUtils;
import com.it.commonutils.MD5;
import com.it.educenter.entity.UcenterMember;
import com.it.educenter.entity.Vo.RegisterVo;
import com.it.educenter.mapper.UcenterMemberMapper;
import com.it.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.servicebase.exceptionhandler.GuliException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author test.java
 * @since 2021-04-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    private RedisTemplate <String,String> redisTemplate;
    //实现登录功能
    public String login(UcenterMember ucenterMember) {
        //获取手机号和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember>wrapper=new QueryWrapper();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //手机号不正确
        if(mobileMember==null){
            throw new GuliException(20001,"手机号不正确");
        }
        //密码不正确
        //输入的密码加密再和数据库比较
        //加密方式MD5
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new GuliException(20001,"密码不正确");
        }
        //用户被禁用
        if(mobileMember.getIsDisabled()){
            throw new GuliException(20001,"用户被禁用");
        }
        //登录成功 生成token字符串
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    //实现注册功能
    public void register(RegisterVo registerVo) {
        //获取注册数据
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码

        //都不能为空
        if(StringUtils.isEmpty(code) ||StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败");
        }

        //验证码正确
        //获取redis验证码
        //String redisCode = redisTemplate.opsForValue().get(mobile);
        String redisCode="1111";
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"验证码错误");
        }

        //手机号不能重复
        QueryWrapper<UcenterMember>wrapper=new QueryWrapper();
        wrapper.eq("mobile",mobile);
        //如果已经有值等于1 没有值等于0
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            throw new GuliException(20001,"手机号重复");
        }

        //注册成功 数据添加到数据库中
        UcenterMember member=new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://guli.shop/photo/banner/1525939573202.jpg");
        baseMapper.insert(member);
    }

    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<UcenterMember>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);

    }
}
